﻿//=============================================================================
//
// 背景 [player.cpp]
//
//=============================================================================
#include "player.h"
#include "input.h"
#include "background.h"
#include "bullet.h"


//*****************************************************************************
// マクロ定義
//*****************************************************************************

////////////////////////////
// 默认模型位置和尺寸
////////////////////////////
#define	PLAYER_MAX					( 4 )						// 玩家の最大数
#define	PLAYER_POS_X				( 400.0f )					// 背景の位置(Ｚ座標)
#define	PLAYER_POS_Y				( 300.0f )					// 背景の位置(Ｚ座標)
#define	PLAYER_SIZE_X				( 50 )			// 背景の幅
#define	PLAYER_SIZE_Y				( 50 )			// 背景の高さ

////////////////////////////
// 01默认位置和尺寸
////////////////////////////
#define	PLAYER_01_POS_X				( 300.0f )					// 背景の位置(Ｚ座標)
#define	PLAYER_01_POS_Y				( 300.0f )					// 背景の位置(Ｚ座標)
#define	PLAYER_01_SIZE_X			( 50.0f )					// 背景の幅
#define	PLAYER_01_SIZE_Y			( 50.0f )					// 背景の高さ


// 图组坐标细分
#define	TEX_PATTERN_SIZE_X			( 1.0f / TEX_PATTERN_DIVIDE_X )		// １パターンのテクスチャサイズ(Ｘ方向)
#define	TEX_PATTERN_SIZE_Y			( 1.0f / TEX_PATTERN_DIVIDE_Y )		// １パターンのテクスチャサイズ(Ｙ方向)

////////////////////////////
// 4行4列图组动画
////////////////////////////
#define	TEX_PATTERN_DIVIDE_X4		( 4 )									// アニメーションパターンのテクスチャ内での分割数(Ｘ方向)
#define	TEX_PATTERN_DIVIDE_Y4		( 4 )									// アニメーションパターンのテクスチャ内での分割数(Ｙ方向)
#define	NUM_ANIM_PATTERN_X4_Y4		( TEX_PATTERN_DIVIDE_X4 * TEX_PATTERN_DIVIDE_Y4 )	// アニメーションのパターン数

#define	ANIM_PATTERN_UP				( 12 )
#define	ANIM_PATTERN_DOWN			( 0 )
#define	ANIM_PATTERN_LEFT			( 4 )
#define	ANIM_PATTERN_RIGHT			( 8 )



////////////////////////////
// 动画播放帧数
////////////////////////////
#define	TIME_CHANGE_PATTERN_PLAYER		( 5 )												// アニメーションの切り替わるタイミング


//*****************************************************************************
// プロトタイプ宣言
//*****************************************************************************
HRESULT MakeVertexPlayer( LPDIRECT3DDEVICE9 pDevice );

void SetVertexPlayer(	int nIdx,									//记录当前对应的图组polygon地址
						float scaleX,								//X轴的放大参数
						float scaleY,								//Y轴的放大参数
						float sizeX,								//自身X轴的大小参数
						float sizeY );								//自身Y轴的大小参数

void SetTexturePlayer(	int nIdx,									//记录当前对应的图组地址
						int nPatternAnim,							//记录当前显示帧
						int numAnimPattern, 						//记录当前对应的组成图组的图片数量
						int texPatternDivideX,						//记录当前图组X轴图片数量
						int texPatternDivideY );					//记录当前图组Y轴图片数量

void SetColorPlayer( int nIdx, D3DCOLOR col_3 );

//*****************************************************************************
// グローバル変数
//*****************************************************************************
LPDIRECT3DTEXTURE9		g_pD3DTexturePlayer[PLAYER_TYPE_MAX] = {};		// テクスチャへのポインタ
LPDIRECT3DVERTEXBUFFER9 g_pD3DVtxBuffPlayer = NULL;		// 頂点バッファインターフェースへのポインタ

PLAYER g_player[PLAYER_MAX];												//

// 计数器 上下左右分开计数
int						g_nCounterAnimPlayer;							// ポリゴンのアニメーションカウンター

//记录图片帧
int						g_nPatternAnimPlayer;						// ポリゴンのアニメーションパターンNo.


//动画播放开关
bool					g_isPlayerAnimPlay;

//移动状态判断开关
bool					g_upP1;
bool					g_downP1;
bool					g_leftP1;
bool					g_rightP1;
bool					g_isMoving;

//////////////////////
// 贴图的种类路径
//////////////////////
const char *c_pTexturePlayer[PLAYER_TYPE_MAX]=			//不同模式下的背景切换
{
	"data/TEXTURE/player/player01.png",
};

//数据保存
int						g_dataCopy01;

float g_radian;

//=============================================================================
// 初期化処理
//=============================================================================
HRESULT InitPlayer( LPDIRECT3DDEVICE9 pDevice )
{
	for( int nCntPlayer = 0; nCntPlayer < PLAYER_MAX; nCntPlayer++ )
	{
		g_player[nCntPlayer].pos = D3DXVECTOR3( PLAYER_POS_X, PLAYER_POS_Y, 0.0f );
		g_player[nCntPlayer].move = D3DXVECTOR3( 5.0f, 5.0f, 0.0f );
		g_player[nCntPlayer].iHP = 0;
		g_player[nCntPlayer].iMP = 0;
		g_player[nCntPlayer].iType = -1;
		g_player[nCntPlayer].iStatus = -1;
		g_player[nCntPlayer].iAnimStatus = -1;
		g_player[nCntPlayer].iBelongTo = -1;
		g_player[nCntPlayer].bUse = false;
		g_player[nCntPlayer].iFaceTo = -1;
		g_player[nCntPlayer].iPatternAnim = 0;
	}

	//计数器初始化
	g_nCounterAnimPlayer = 0;

	// 动画帧初始化
	g_nPatternAnimPlayer = 0;


	//动画播放开关初始化
	g_isPlayerAnimPlay = false;

	//移动状态判断开关初始化
	g_upP1 = false;
	g_downP1 = false;
	g_leftP1 = false;
	g_rightP1 = false;

	g_isMoving = false;



	//数据临时缓存
	g_dataCopy01 = 0;

	// 頂点情報の作成
	MakeVertexPlayer( pDevice );

	// テクスチャの読み込み
	for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
	{
		D3DXCreateTextureFromFile(	pDevice,								// デバイスへのポインタ
									c_pTexturePlayer[nCntPlayer],				// ファイルの名前
									&g_pD3DTexturePlayer[nCntPlayer] );		// 読み込むメモリー
	}



	SetPlayer(	D3DXVECTOR3( PLAYER_POS_X, PLAYER_POS_Y, 0.0f ),						// 位置
				D3DXVECTOR3( 5.0, 5.0, 0.0f ),											// 位移
				10,									// 血量
				10,									// 魔法
				PLAYER_01,								// 类型
				P1_SET );							// 属于哪个玩家




	return S_OK;
}

//=============================================================================
// 終了処理
//=============================================================================
void UninitPlayer( void )
{
	for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
	{
		if( g_pD3DTexturePlayer[nCntPlayer] != NULL )
		{// テクスチャの開放
			g_pD3DTexturePlayer[nCntPlayer]->Release();
			g_pD3DTexturePlayer[nCntPlayer] = NULL;
		}
	}

	if( g_pD3DVtxBuffPlayer != NULL )
	{// 頂点バッファの開放
		g_pD3DVtxBuffPlayer->Release();
		g_pD3DVtxBuffPlayer = NULL;
	}
}

//
//void UpdatePlayer(void)
//{
//	for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
//	{
//		if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
//		{
//			g_player[nCntPlayer].pos.x = cos( g_radian ) * 100.0f + PLAYER_01_POS_X;
//			g_player[nCntPlayer].pos.y = sin( g_radian ) * 100.0f + PLAYER_01_POS_Y;
//		}
//	}
//
//	
//	g_radian += (3.14 / 180);
//
//
//	if( g_radian == 3.14 )
//	{
//		g_radian = 0;
//	}
//
//
//
//	SetVertexPlayer(PLAYER_01, 0, 0, PLAYER_01_SIZE_X, PLAYER_01_SIZE_Y);
//	SetTexturePlayer(PLAYER_01, g_nPatternAnimPlayer, NUM_ANIM_PATTERN_X4_Y4, TEX_PATTERN_DIVIDE_X4, TEX_PATTERN_DIVIDE_Y4);
//}









//=============================================================================
// 更新処理
//=============================================================================
void UpdatePlayer(void)
{
	//获取地图位置信息
	BACKGROUND *backgroundPos = GetBGPos();
	BULLET *pBulletPos = GetBulletPos();

	//动画播放计数器
	g_nCounterAnimPlayer++;								// ポリゴンのアニメーションカウンター







	//for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
	//{
	//	if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
	//	{
	//		g_player[nCntPlayer].pos.x = GetMouseX();
	//		g_player[nCntPlayer].pos.y = GetMouseY();
	//	}
	//}






	//if( IsMouseRightTriggered() )
	//{
	//	//移动开关打开
	//	g_isMoving = true;
	//}

	////如果移动开关打开就移动，到指定位置后停止
	//if( g_isMoving == true )
	//{
	//	for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
	//	{
	//		if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
	//		{
	//			if( g_player[nCntPlayer].pos.x < GetMouseX() )
	//			{
	//				g_player[nCntPlayer].pos.x--;
	//			}
	//			else
	//			{
	//				g_player[nCntPlayer].pos.x++;
	//			}

	//			if( g_player[nCntPlayer].pos.y < GetMouseY() )
	//			{
	//				g_player[nCntPlayer].pos.y--;
	//			}
	//			else
	//			{
	//				g_player[nCntPlayer].pos.y++;
	//			}

	//			if( g_player[nCntPlayer].pos.x == GetMouseX() &&
	//				g_player[nCntPlayer].pos.y == GetMouseY())
	//			{
	//				g_isMoving == false;
	//			}
	//		}
	//	}
	//}





	//如果玩家不输入任何方向键，那么初始化玩家动画状态值
	if( GetKeyboardPress(DIK_W) == false &&
		GetKeyboardPress(DIK_S) == false &&
		GetKeyboardPress(DIK_A) == false &&
		GetKeyboardPress(DIK_D) == false )
	{
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			g_player[nCntPlayer].iAnimStatus = -1;
		}

		//方向判断初始化
		g_upP1 = false;
		g_downP1 = false;
		g_leftP1 = false;
		g_rightP1 = false;
	}



	////////////////////////////
	//这里只定义状态不改变移动方位
	////////////////////////////
	if( GetKeyboardPress(DIK_W) && g_downP1 == false && g_leftP1 == false && g_rightP1 == false )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				g_player[nCntPlayer].iAnimStatus = PLAYER_UP;
				g_player[nCntPlayer].iFaceTo = PLAYER_UP;
				g_upP1 = true;
			}
		}
	}
	//如果该按键弹起则复位
	else if( GetKeyboardPress(DIK_W) == false )
	{
		g_upP1 = false;
	}


	if( GetKeyboardPress(DIK_S) && g_leftP1 == false && g_rightP1 == false && g_upP1 == false )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				g_player[nCntPlayer].iAnimStatus = PLAYER_DOWN;
				g_player[nCntPlayer].iFaceTo = PLAYER_DOWN;
				g_downP1 = true;
			}
		}
	}
	//如果该按键弹起则复位
	else if( GetKeyboardPress(DIK_S) == false )
	{
		g_downP1 = false;
	}

	if( GetKeyboardPress(DIK_A) && g_downP1 == false && g_rightP1 == false && g_upP1 == false )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				g_player[nCntPlayer].iAnimStatus = PLAYER_LEFT;
				g_player[nCntPlayer].iFaceTo = PLAYER_LEFT;
				g_leftP1 = true;
			}
		}
	}
	//如果该按键弹起则复位
	else if( GetKeyboardPress(DIK_A) == false )
	{
		g_leftP1 = false;
	}

	if( GetKeyboardPress(DIK_D)  && g_downP1 == false && g_leftP1 == false && g_upP1 == false)
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				g_player[nCntPlayer].iAnimStatus = PLAYER_RIGHT;
				g_player[nCntPlayer].iFaceTo = PLAYER_RIGHT;
				g_rightP1 = true;
			}
		}
	}
	//如果该按键弹起则复位
	else if( GetKeyboardPress(DIK_D) == false )
	{
		g_rightP1 = false;
	}




	///////////////////////////////////
	//如果同时按下两个方向键的情况下
	///////////////////////////////////
	if( GetKeyboardPress(DIK_W) && GetKeyboardPress(DIK_A) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//根据玩家动作状态选择播放动画
				//如果玩家之前状态等于左，那么改为左上
				if( g_leftP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_LEFT_UP;
					g_player[nCntPlayer].iFaceTo = PLAYER_LEFT;
				}
				//如果玩家之前状态等于上，那么改为上左
				else if( g_upP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_UP_LEFT;
					g_player[nCntPlayer].iFaceTo = PLAYER_UP;
				}
			}
		}
	}

	if( GetKeyboardPress(DIK_W) && GetKeyboardPress(DIK_D) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//根据玩家动作状态选择播放动画
				//如果玩家之前状态等于右，那么改为右上
				if( g_rightP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_RIGHT_UP;
					g_player[nCntPlayer].iFaceTo = PLAYER_RIGHT;
				}
				//如果玩家之前状态等于上，那么改为上右
				else if( g_upP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_UP_RIGHT;
					g_player[nCntPlayer].iFaceTo = PLAYER_UP;
				}
			}
		}
	}

	if( GetKeyboardPress(DIK_S) && GetKeyboardPress(DIK_A) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//根据玩家动作状态选择播放动画
				//如果玩家之前状态等于左，那么改为左上
				if( g_leftP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_LEFT_DOWN;
					g_player[nCntPlayer].iFaceTo = PLAYER_LEFT;
				}
				//如果玩家之前状态等于上，那么改为上左
				else if( g_downP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_DOWN_LEFT;
					g_player[nCntPlayer].iFaceTo = PLAYER_DOWN;
				}
			}
		}
	}

	if( GetKeyboardPress(DIK_S) && GetKeyboardPress(DIK_D) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//根据玩家动作状态选择播放动画
				//如果玩家之前状态等于右，那么改为右上
				if( g_rightP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_RIGHT_DOWN;
					g_player[nCntPlayer].iFaceTo = PLAYER_RIGHT;
				}
				//如果玩家之前状态等于上，那么改为上右
				else if( g_downP1 == true )
				{
					g_player[nCntPlayer].iAnimStatus = PLAYER_DOWN_RIGHT;
					g_player[nCntPlayer].iFaceTo = PLAYER_DOWN;
				}
			}
		}
	}




	////////////////////////////////////////
	//这里根据状态来判定动画并且位移
	////////////////////////////////////////
	//单方向判断
	for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
	{
		if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
		{
			if( g_player[nCntPlayer].iAnimStatus == PLAYER_UP )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_UP;
				}
				g_player[nCntPlayer].pos.y -= g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_DOWN )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_DOWN;
				}
				g_player[nCntPlayer].pos.y += g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_LEFT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_LEFT;
				}
				g_player[nCntPlayer].pos.x -= g_player[nCntPlayer].move.x;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_RIGHT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_RIGHT;
				}
				g_player[nCntPlayer].pos.x += g_player[nCntPlayer].move.x;
			}






			//双方向判断
			if( g_player[nCntPlayer].iAnimStatus == PLAYER_LEFT_UP )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_LEFT;
				}
				g_player[nCntPlayer].pos -= g_player[nCntPlayer].move;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_UP_LEFT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_UP;
				}
				g_player[nCntPlayer].pos -= g_player[nCntPlayer].move;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_RIGHT_UP )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_RIGHT;
				}
				g_player[nCntPlayer].pos.x += g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y -= g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_UP_RIGHT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_UP;
				}
				g_player[nCntPlayer].pos.x += g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y -= g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_LEFT_DOWN )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_LEFT;
				}
				g_player[nCntPlayer].pos.x -= g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y += g_player[nCntPlayer].move.y;

			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_DOWN_LEFT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_DOWN;
				}
				g_player[nCntPlayer].pos.x -= g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y += g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_RIGHT_DOWN )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_RIGHT;
				}
				g_player[nCntPlayer].pos.x += g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y += g_player[nCntPlayer].move.y;
			}

			if( g_player[nCntPlayer].iAnimStatus == PLAYER_DOWN_RIGHT )
			{
				if( (g_nCounterAnimPlayer % TIME_CHANGE_PATTERN_PLAYER) == 0 )
				{
					g_nPatternAnimPlayer = ( g_nPatternAnimPlayer + 1 ) % TEX_PATTERN_DIVIDE_X4 + ANIM_PATTERN_DOWN;
				}
				g_player[nCntPlayer].pos.x += g_player[nCntPlayer].move.x;
				g_player[nCntPlayer].pos.y += g_player[nCntPlayer].move.y;
			}
		}
	}



	//////////////////////////////////////
	// 根据玩家输入发出子弹
	//////////////////////////////////////
	if( GetKeyboardTrigger(DIK_J) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				if( g_player[nCntPlayer].iFaceTo == PLAYER_UP )
				{
					SetBullet(	g_player[nCntPlayer].pos,				// 位置
								D3DXVECTOR3(0.0f, -0.5f, 0.0f),			// 位移
								BULLET_01,								// 类型
								P1_SET );								// 属于哪个玩家
				}

				if( g_player[nCntPlayer].iFaceTo == PLAYER_DOWN )
				{
					SetBullet(	g_player[nCntPlayer].pos,				// 位置
								D3DXVECTOR3(0.0f, 0.5f, 0.0f),			// 位移
								BULLET_01,								// 类型
								P1_SET );								// 属于哪个玩家
				}

				if( g_player[nCntPlayer].iFaceTo == PLAYER_LEFT )
				{
					SetBullet(	g_player[nCntPlayer].pos,				// 位置
								D3DXVECTOR3(-0.5f, 0.0f, 0.0f),			// 位移
								BULLET_01,								// 类型
								P1_SET );								// 属于哪个玩家
				}

				if( g_player[nCntPlayer].iFaceTo == PLAYER_RIGHT )
				{
					SetBullet(	g_player[nCntPlayer].pos,				// 位置
								D3DXVECTOR3(0.5f, 0.0f, 0.0f),			// 位移
								BULLET_01,								// 类型
								P1_SET );								// 属于哪个玩家
				}
			}
		}
	}

	if( GetKeyboardTrigger(DIK_K) )
	{	
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
					SetBullet(	g_player[nCntPlayer].pos,				// 位置
								D3DXVECTOR3(0.0f, 0.0f, 0.0f),			// 位移
								BULLET_02,								// 类型
								P1_SET );								// 属于哪个玩家
				}
		}
	}


















	SetVertexPlayer(PLAYER_01, 0, 0, PLAYER_01_SIZE_X, PLAYER_01_SIZE_Y);
	SetTexturePlayer(PLAYER_01, g_nPatternAnimPlayer, NUM_ANIM_PATTERN_X4_Y4, TEX_PATTERN_DIVIDE_X4, TEX_PATTERN_DIVIDE_Y4);
	






















	//////////////////////////////////////
	// 控制玩家移动和地图滚动的设定
	//////////////////////////////////////
	//【左】键输入的情况下
	if( GetKeyboardPress(DIK_A) )
	{
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//如果地图尚未滚动到【左】边界的话，人物与地图保持一定的距离
				if( backgroundPos->pos.x < BG_BORDER_LEFT_X )
				{
					if( g_player[nCntPlayer].pos.x < ( PLAYER_01_SIZE_X / 2 + 150) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.x = ( PLAYER_01_SIZE_X / 2 + 150);		// プレイヤーは出ないように。
					}
				}
				//如果地图滚动到左边边界的话，人物可以走到地图边上
				else if( backgroundPos->pos.x == BG_BORDER_LEFT_X )
				{
					if( g_player[nCntPlayer].pos.x < ( PLAYER_01_SIZE_X / 2 ) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.x = ( PLAYER_01_SIZE_X / 2 );		// プレイヤーは出ないように。
					}
				}
				//如果玩家靠近屏幕左边
				if( g_player[nCntPlayer].pos.x <= 200 )
				{
					//如果地图尚未滚动到左边边界的话，地图滚动
					if( backgroundPos->pos.x < BG_BORDER_LEFT_X )
					{
						backgroundPos->pos.x += 5;

						*GetIsBulletLeft() = true;

						//for( int nCntBullet = 0; nCntBullet < BULLET_MAX; nCntBullet++, pBulletPos++ )
						//{
						//	pBulletPos->pos.x += 5;
						//}
					}
				}
			}
		}
	}

	//【右】键输入的情况下
	if( GetKeyboardPress(DIK_D) )
	{
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//如果地图尚未滚动到【右】边界的话，人物与地图保持一定的距离
				if( backgroundPos->pos.x > BG_BORDER_RIGHT_X )
				{
					if( g_player[nCntPlayer].pos.x > ( SCREEN_WIDTH - PLAYER_01_SIZE_X / 2 - 150) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.x = ( SCREEN_WIDTH - PLAYER_01_SIZE_X / 2 - 150);		// プレイヤーは出ないように。
					}
				}
				//如果地图滚动到【右】边界的话，人物可以走到地图边上
				else if( backgroundPos->pos.x == BG_BORDER_RIGHT_X )
				{
					if( g_player[nCntPlayer].pos.x > ( SCREEN_WIDTH - PLAYER_01_SIZE_X / 2 ) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.x = ( SCREEN_WIDTH - PLAYER_01_SIZE_X / 2 );		// プレイヤーは出ないように。
					}
				}
				//如果玩家靠近屏幕【右】
				if( g_player[nCntPlayer].pos.x >= SCREEN_WIDTH - 200 )
				{
					//如果地图尚未滚动到【右】边界的话，地图滚动
					if( backgroundPos->pos.x > BG_BORDER_RIGHT_X )
					{
						backgroundPos->pos.x -= 5;

						*GetIsBulletRight() = true;

						//for( int nCntBullet = 0; nCntBullet < BULLET_MAX; nCntBullet++, pBulletPos++ )
						//{
						//	pBulletPos->pos.x -= 5;
						//}
					}
				}
			}
		}
	}

	//【上】键输入的情况下
	if( GetKeyboardPress(DIK_W) )
	{
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//如果地图尚未滚动到【上】边界的话，人物与地图保持一定的距离
				if(backgroundPos->pos.y < BG_BORDER_UP_Y)
				{
					if( g_player[nCntPlayer].pos.y < ( PLAYER_01_SIZE_Y / 2 + 100) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.y = ( PLAYER_01_SIZE_Y / 2 + 100);		// プレイヤーは出ないように。
					}
				}
				//如果地图滚动到【上】边界的话，人物可以走到地图边上
				else if(backgroundPos->pos.y == BG_BORDER_UP_Y)
				{
					if( g_player[nCntPlayer].pos.y < ( PLAYER_01_SIZE_Y / 2 ) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.y = ( PLAYER_01_SIZE_Y / 2 );		// プレイヤーは出ないように。
					}
				}
				//如果玩家靠近屏幕【上】
				if( g_player[nCntPlayer].pos.y <= 150 )
				{
					//如果地图尚未滚动到【上】边界的话，地图滚动
					if(backgroundPos->pos.y < BG_BORDER_UP_Y)
					{
						backgroundPos->pos.y += 5;

						*GetIsBulletUp() = true;

						//for( int nCntBullet = 0; nCntBullet < BULLET_MAX; nCntBullet++, pBulletPos++ )
						//{
						//	pBulletPos->pos.y += 5;
						//}
					}
				}
			}
		}
	}

	//【下】键输入的情况下
	if( GetKeyboardPress(DIK_S) )
	{
		for( int nCntPlayer = 0; nCntPlayer < PLAYER_TYPE_MAX; nCntPlayer++ )
		{
			if( g_player[nCntPlayer].bUse && g_player[nCntPlayer].iBelongTo == P1_SET )
			{
				//如果地图尚未滚动到【下】边界的话，人物与地图保持一定的距离
				if(backgroundPos->pos.y > BG_BORDER_DOWN_Y)
				{
					if( g_player[nCntPlayer].pos.y > ( SCREEN_HEIGHT - PLAYER_01_SIZE_Y / 2 - 100) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.y = ( SCREEN_HEIGHT - PLAYER_01_SIZE_Y / 2 - 100);		// プレイヤーは出ないように。
					}
				}
				//如果地图滚动到【下】边界的话，人物可以走到地图边下
				else if(backgroundPos->pos.y == BG_BORDER_DOWN_Y)
				{
					if( g_player[nCntPlayer].pos.y > ( SCREEN_HEIGHT - PLAYER_01_SIZE_Y / 2 ) )	// プレイヤーはウインドウの高さウインドウより大きい場合。
					{
						g_player[nCntPlayer].pos.y = ( SCREEN_HEIGHT - PLAYER_01_SIZE_Y / 2 );		// プレイヤーは出ないように。
					}
				}
				//如果玩家靠近屏幕【下】
				if( g_player[nCntPlayer].pos.y >= SCREEN_HEIGHT - 150 )
				{
					//如果地图尚未滚动到【下】边界的话，地图滚动
					if(backgroundPos->pos.y > BG_BORDER_DOWN_Y)
					{
						backgroundPos->pos.y -= 5;

						*GetIsBulletDown() = true;

						//for( int nCntBullet = 0; nCntBullet < BULLET_MAX; nCntBullet++, pBulletPos++ )
						//{
						//	pBulletPos->pos.y -= 5;
						//}
					}
				}
			}
		}
	}


}










//=============================================================================
// 描画処理
//=============================================================================
void DrawPlayer( LPDIRECT3DDEVICE9 pDevice )
{
	// 頂点バッファをデバイスのデータストリームにバインド
	pDevice->SetStreamSource( 0, g_pD3DVtxBuffPlayer, 0, sizeof(VERTEX_2D) );

	// 頂点フォーマットの設定
	pDevice->SetFVF( FVF_VERTEX_2D );

	for( int nCntPlayer = 0; nCntPlayer < PLAYER_MAX; nCntPlayer++ )
	{
		if( g_player[nCntPlayer].bUse )
		{
				// テクスチャの設定
			pDevice->SetTexture( 0, g_pD3DTexturePlayer[g_player[nCntPlayer].iType] );

			// ポリゴンの描画
			pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, (nCntPlayer * 4), NUM_POLYGON );
		}
	}
}

//=============================================================================
// 頂点の作成
//=============================================================================
HRESULT MakeVertexPlayer( LPDIRECT3DDEVICE9 pDevice )
{
	// オブジェクトの頂点バッファを生成
	if( FAILED( pDevice->CreateVertexBuffer(sizeof(VERTEX_2D) * NUM_VERTEX * PLAYER_MAX,		// 頂点データ用に確保するバッファサイズ(バイト単位)
												D3DUSAGE_WRITEONLY,				// 頂点バッファの使用法　
												FVF_VERTEX_2D,					// 使用する頂点フォーマット
												D3DPOOL_MANAGED,				// リソースのバッファを保持するメモリクラスを指定
												&g_pD3DVtxBuffPlayer,				// 頂点バッファインターフェースへのポインタ
												NULL)) )							// NULLに設定
	{
		return E_FAIL;
	}

	{//頂点バッファの中身を埋める
		VERTEX_2D *pVtx;

		// 頂点データの範囲をロックし、頂点バッファへのポインタを取得
		g_pD3DVtxBuffPlayer->Lock(0, 0, (void**)&pVtx, 0);


		for( int nCntPlayer = 0; nCntPlayer < PLAYER_MAX; nCntPlayer++, pVtx += 4 )
		{
		// 頂点座標の設定
			//pVtx[0].vtx = D3DXVECTOR3(g_player[PLAYER_MAX].pos.x - PLAYER_SIZE_X / 2, g_player[PLAYER_MAX].pos.y - PLAYER_SIZE_Y / 2, 0.0f);
			//pVtx[1].vtx = D3DXVECTOR3(g_player[PLAYER_MAX].pos.x + PLAYER_SIZE_X / 2, g_player[PLAYER_MAX].pos.y - PLAYER_SIZE_Y / 2, 0.0f);
			//pVtx[2].vtx = D3DXVECTOR3(g_player[PLAYER_MAX].pos.x - PLAYER_SIZE_X / 2, g_player[PLAYER_MAX].pos.y + PLAYER_SIZE_Y / 2, 0.0f);
			//pVtx[3].vtx = D3DXVECTOR3(g_player[PLAYER_MAX].pos.x + PLAYER_SIZE_X / 2, g_player[PLAYER_MAX].pos.y + PLAYER_SIZE_Y / 2, 0.0f);

			// rhwの設定
			pVtx[0].rhw = 1.0f;
			pVtx[1].rhw = 1.0f;
			pVtx[2].rhw = 1.0f;
			pVtx[3].rhw = 1.0f;

			// 反射光の設定
			pVtx[0].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255);
			pVtx[1].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255);
			pVtx[2].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255);
			pVtx[3].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255);

			//// テクスチャ座標の設定
			//pVtx[0].tex = D3DXVECTOR2(0.0f, 0.0f);
			//pVtx[1].tex = D3DXVECTOR2(TEX_PATTERN_SIZE_X, 0.0f);
			//pVtx[2].tex = D3DXVECTOR2(0.0f, TEX_PATTERN_SIZE_Y);
			//pVtx[3].tex = D3DXVECTOR2(TEX_PATTERN_SIZE_X, TEX_PATTERN_SIZE_Y);

			// 頂点データをアンロックする
			g_pD3DVtxBuffPlayer->Unlock();
		}
	}

	return S_OK;
}

void SetVertexPlayer(	int nIdx,					//记录当前对应的图组polygon地址
						float scaleX,				//X轴的放大参数
						float scaleY,				//Y轴的放大参数
						float sizeX,				//自身X轴的大小参数
						float sizeY )				//自身Y轴的大小参数
{
	VERTEX_2D *pVtx;

	// 頂点データの範囲をロックし、頂点バッファへのポインタを取得
	g_pD3DVtxBuffPlayer->Lock( 0, 0, (void**)&pVtx, 0 );

	pVtx += (nIdx * 4);

	// 頂点座標の設定
	// 这里的放大效果是以中心向四周放大
	pVtx[0].vtx = D3DXVECTOR3( g_player[nIdx].pos.x - scaleX - sizeX / 2, g_player[nIdx].pos.y - scaleY - sizeY / 2, 0.0f );
	pVtx[1].vtx = D3DXVECTOR3( g_player[nIdx].pos.x + scaleX + sizeX / 2, g_player[nIdx].pos.y - scaleY - sizeY / 2, 0.0f );
	pVtx[2].vtx = D3DXVECTOR3( g_player[nIdx].pos.x - scaleX - sizeX / 2, g_player[nIdx].pos.y + scaleY + sizeY / 2, 0.0f );
	pVtx[3].vtx = D3DXVECTOR3( g_player[nIdx].pos.x + scaleX + sizeX / 2, g_player[nIdx].pos.y + scaleY + sizeY / 2, 0.0f );


	// 頂点データをアンロックする
	g_pD3DVtxBuffPlayer->Unlock();
}




void SetTexturePlayer(	int nIdx,						//记录当前对应的图组地址
						int nPatternAnim,				//记录当前显示帧
						int numAnimPattern, 			//记录当前对应的组成图组的图片数量
						int texPatternDivideX,			//记录当前图组X轴图片数量
						int texPatternDivideY )			//记录当前图组Y轴图片数量
{
	{//頂点バッファの中身を埋める
		VERTEX_2D *pVtx;
		float fPosXLeft, fPosXRight;
		float fPosYUp, fPosYDown;

		// 頂点データの範囲をロックし、頂点バッファへのポインタを取得
		g_pD3DVtxBuffPlayer->Lock(0, 0, (void**)&pVtx, 0);

		pVtx += (nIdx * 4);

		// テクスチャ座標の設定 纹理坐标设定
		fPosXLeft = (float)(nPatternAnim % texPatternDivideX) * 1.0f / texPatternDivideX;
		fPosXRight = (float)(nPatternAnim % texPatternDivideX) * 1.0f / texPatternDivideX + 1.0f / texPatternDivideX;
		fPosYUp = (float)(nPatternAnim / (numAnimPattern / texPatternDivideY)) * 1.0f / texPatternDivideY;
		fPosYDown = (float)(nPatternAnim / (numAnimPattern / texPatternDivideY)) * 1.0f / texPatternDivideY + 1.0f / texPatternDivideY;

		pVtx[0].tex = D3DXVECTOR2(fPosXLeft, fPosYUp);
		pVtx[1].tex = D3DXVECTOR2(fPosXRight, fPosYUp);
		pVtx[2].tex = D3DXVECTOR2(fPosXLeft, fPosYDown);
		pVtx[3].tex = D3DXVECTOR2(fPosXRight, fPosYDown);

		// 頂点データをアンロックする
		g_pD3DVtxBuffPlayer->Unlock();
	}
}

void SetColorPlayer( int nIdx, D3DCOLOR col_3 )
{
	VERTEX_2D *pVtx;

	// 頂点データの範囲をロックし、頂点バッファへのポインタを取得
	g_pD3DVtxBuffPlayer->Lock(0, 0, (void**)&pVtx, 0);

	pVtx += ( nIdx * 4 );

	// 頂点座標の設定
	pVtx[0].diffuse =
	pVtx[1].diffuse =
	pVtx[2].diffuse =
	pVtx[3].diffuse = col_3;

	// 頂点データをアンロックする
	g_pD3DVtxBuffPlayer->Unlock();
}


void SetPlayer( D3DXVECTOR3 pos,						// 位置
				D3DXVECTOR3 move,						// 位移
				int HP,									// 血量
				int MP,									// 魔法
				int nType,								// 类型
				int belongTo )							// 属于哪个玩家
{
	for(int nCntPlayer = 0; nCntPlayer < PLAYER_MAX; nCntPlayer++)
	{
		if(!g_player[nCntPlayer].bUse)
		{
			g_player[nCntPlayer].pos = pos;
			g_player[nCntPlayer].iBelongTo = belongTo;
			g_player[nCntPlayer].iType = nType;


			SetVertexPlayer(nType, 0, 0, PLAYER_01_SIZE_X, PLAYER_01_SIZE_Y);

			




			//switch(g_player[nCntPlayer].nType)
			//{
			//case ENEMYTYPE_000:
			//	break;

			//case ENEMYTYPE_001:
			//	break;

			//case ENEMYTYPE_002:
			//	break;

			//case ENEMYTYPE_003:
			//	break;
			//}

			g_player[nCntPlayer].bUse = true;

			break;
		}
	}
}


PLAYER *GetPlayerPos(void)
{
	return &g_player[0];
}